package fib

import (
	"bufio"
	"fmt"
	"io"
	"strings"
)

func Fibonacci2() intGen {
	a, b := 0, 1
	return func() int {
		tmp := a
		a = b
		b = tmp + b
		//a, b := b, a+b
		return a
	}
}

// 函数可以实现接口
type intGen func() int

func (g intGen) Read(p []byte) (n int, err error) {
	next := g()
	if next > 10000 {
		return 0, io.EOF
	}
	s := fmt.Sprintf("%d\n", next)

	//TODO: 数字过大之后怎么弄

	return strings.NewReader(s).Read(p)
}

func printFileContents(reader io.Reader) {
	scanner := bufio.NewScanner(reader)

	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

//func main() {
//	f := Fibonacci2()
//
//	printFileContents(f)
//}
